﻿using System.Collections;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SL8.SL.Helpers;
using System;

namespace SL8.SL.Controls
{
    public abstract class SearchItemBase : ContentControl, ISearchItem, IControl
    {
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title",
            typeof(string),
            typeof(SearchItemBase), null);

        public string Title
        {
            get
            {
                return (string)base.GetValue(SearchItemBase.TitleProperty);
            }

            set
            {
                base.SetValue(SearchItemBase.TitleProperty, value);
            }
        }

        public static readonly DependencyProperty ParameterExpressionProperty = DependencyProperty.Register("ParameterExpression",
            typeof(System.Linq.Expressions.ParameterExpression),
            typeof(SearchItemBase), null);

        public System.Linq.Expressions.ParameterExpression ParameterExpression
        {
            get
            {
                return (ParameterExpression)base.GetValue(SearchItemBase.ParameterExpressionProperty);
            }
            set
            {
                base.SetValue(SearchItemBase.ParameterExpressionProperty, value);
            }
        }

        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",
            typeof(IEnumerable),
            typeof(SearchItemBase), null);

        public IEnumerable ItemsSource
        {
            get
            {
                return (IEnumerable)base.GetValue(SearchItemBase.ItemsSourceProperty);
            }

            set
            {
                base.SetValue(SearchItemBase.ItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty SearchCommandProperty = DependencyProperty.Register("SearchCommand",
            typeof(ICommand),
            typeof(SearchItemBase), null);

        public ICommand SearchCommand
        {
            get
            {
                return (ICommand)base.GetValue(SearchItemBase.SearchCommandProperty);
            }

            set
            {
                base.SetValue(SearchItemBase.SearchCommandProperty, value);
            }
        }

        public static readonly DependencyProperty EntityTypeProperty = DependencyProperty.Register("EntityType",
            typeof(Type),
            typeof(SearchItemBase), null);

        public System.Type EntityType
        {
            get
            {
                return (Type)base.GetValue(SearchItemBase.EntityTypeProperty);
            }

            set
            {
                base.SetValue(SearchItemBase.EntityTypeProperty, value);
            }
        }

        public static readonly DependencyProperty CompareTypeProperty = DependencyProperty.Register("CompareType",
            typeof(CompareType),
            typeof(SearchItemBase), new PropertyMetadata(CompareType.Equal));

        public CompareType CompareType
        {
            get
            {
                return (CompareType)base.GetValue(SearchItemBase.CompareTypeProperty);
            }

            set
            {
                base.SetValue(SearchItemBase.CompareTypeProperty, value);
            }
        }


        public event SearchEventHandler Search;

        public virtual System.Linq.Expressions.LambdaExpression GetSearchExpression()
        {
            return null;
        }

        public virtual void SubscribeToTemplatePartEvents()
        {
        }

        public virtual void UnsubscribeFromTemplatePartEvents()
        {
        }

        public virtual void ToSearch()
        {
            System.Linq.Expressions.Expression expression = this.GetSearchExpression();

            if (this.Search != null)
            {
                this.Search(this, new SearchEventArgs()
                {
                    SearchExpression = expression
                });
            }

            if (this.SearchCommand != null)
            {
                this.SearchCommand.Execute(expression);
            }
        }
    }
}
